package org.cloud.im.server.demo;

import org.apache.commons.lang3.StringUtils;
import org.cloud.im.core.packets.Command;
import org.cloud.im.core.utils.PropUtil;
import org.cloud.im.server.CloudImServer;
import org.cloud.im.server.command.CommandManager;
import org.cloud.im.server.command.handler.ChatReqHandler;
import org.cloud.im.server.command.handler.HandshakeReqHandler;
import org.cloud.im.server.command.handler.LoginReqHandler;
import org.cloud.im.server.config.ImServerConfig;
import org.cloud.im.server.config.PropertyImServerConfigBuilder;
import org.cloud.im.server.demo.command.DemoWsHandshakeProcessor;
import org.cloud.im.server.demo.listener.ImDemoGroupListener;
import org.cloud.im.server.demo.listener.ImDemoUserListener;
import org.cloud.im.server.demo.service.LoginServiceProcessor;
import org.cloud.im.server.processor.chat.DefaultAsyncChatMessageProcessor;
import org.tio.core.ssl.SslConfig;

/**
 * @Author: w.weidong.lin@abite.com
 * @Description: IM服务端DEMO演示启动类
 * @Date: create in 2021/10/20 9:17
 */
public class ImServerDemoStart {
    public static void main(String[] args) throws Exception {
        ImServerConfig imServerConfig = new PropertyImServerConfigBuilder("config/im.properties").build();
        //初始化SSL;(开启SSL之前,你要保证你有SSL证书哦...)
        initSsl(imServerConfig);
        //设置群组监听器，非必须，根据需要自己选择性实现;
        imServerConfig.setImGroupListener(new ImDemoGroupListener());
        //设置绑定用户监听器，非必须，根据需要自己选择性实现;
        imServerConfig.setImUserListener(new ImDemoUserListener());
        CloudImServer cloudImServer = new CloudImServer(imServerConfig);

        /*****************start 以下处理器根据业务需要自行添加与扩展，每个Command都可以添加扩展,此处为demo中处理**********************************/

        HandshakeReqHandler handshakeReqHandler = CommandManager.getCommand(Command.COMMAND_HANDSHAKE_REQ, HandshakeReqHandler.class);
        //添加自定义握手处理器;
        handshakeReqHandler.addMultiProtocolProcessor(new DemoWsHandshakeProcessor());
        LoginReqHandler loginReqHandler = CommandManager.getCommand(Command.COMMAND_LOGIN_REQ, LoginReqHandler.class);
        //添加登录业务处理器;
        loginReqHandler.setSingleProcessor(new LoginServiceProcessor());
        //添加用户业务聊天记录处理器，用户自己继承抽象类BaseAsyncChatMessageProcessor即可，以下为内置默认的处理器！
        ChatReqHandler chatReqHandler = CommandManager.getCommand(Command.COMMAND_CHAT_REQ, ChatReqHandler.class);
        chatReqHandler.setSingleProcessor(new DefaultAsyncChatMessageProcessor());
        /*****************end *******************************************************************************************/
        cloudImServer.start();
    }

    /**
     * 开启SSL之前，你要保证你有SSL证书哦！
     *
     * @param imServerConfig
     * @throws Exception
     */
    private static void initSsl(ImServerConfig imServerConfig) throws Exception {
        //开启SSL
        if (ImServerConfig.ON.equals(imServerConfig.getIsSSL())) {
            String keyStorePath = PropUtil.get("cloudIm.key.store.path");
            String keyStoreFile = keyStorePath;
            String trustStoreFile = keyStorePath;
            String keyStorePwd = PropUtil.get("cloudIm.key.store.pwd");
            if (StringUtils.isNotBlank(keyStoreFile) && StringUtils.isNotBlank(trustStoreFile)) {
                SslConfig sslConfig = SslConfig.forServer(keyStoreFile, trustStoreFile, keyStorePwd);
                imServerConfig.setSslConfig(sslConfig);
            }
        }
    }
}
